package com.yxx.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.sys.common.*;
import com.yxx.sys.domain.Dept;
import com.yxx.sys.domain.Permission;
import com.yxx.sys.domain.Permission;
import com.yxx.sys.domain.User;
import com.yxx.sys.service.PermissionService;
import com.yxx.sys.vo.PermissionVO;
import com.yxx.sys.vo.PermissionVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2020-08-13
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

  @Autowired
  private PermissionService permissionService;



  @RequestMapping(value = "/loadIndexLeftMenuJson")
  @ApiOperation(value="加载左侧菜单接口", notes="加载左侧菜单")
  public DataGridView loadIndexLeftMenuJson(){
    //查询所有菜单
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    //设置只查询可获得的菜单
    queryWrapper.eq("type", Constast.TYPE_MENU);
    queryWrapper.eq("available", Constast.AVAILABLE_TRUE);

    User user = (User) WebUtils.getSession().getAttribute("user");
    List<Permission> list=null;
    if(user.getType()==Constast.USER_TYPE_SUPER){
       list= permissionService.list(queryWrapper);
    }else {
      //根据用户id+角色+权限去查
      list= permissionService.list(queryWrapper);
    }


    List<TreeNode> treeNodes = new ArrayList<>();
    for(Permission p:list){
      TreeNode e = new TreeNode();
      e.setId(p.getId());
      e.setParentId(p.getPid());
      e.setIcon(p.getIcon());
      e.setHref(p.getHref());
      e.setTitle(p.getTitle());
      e.setSpread(p.getOpen()==Constast.OPEN_TRUE?true:false);
      treeNodes.add(e);
    }
    //构建层级关系
    List<TreeNode> list2 = TreeNodeBuilder.build(treeNodes, 1);

    return new DataGridView(list2);
  }


  /*
  * 菜单管理
  * */


  /***
   *@Description: 加载菜单管理左侧的菜单树json
   * @param permissionVO
   * @return: com.yxx.sys.common.DataGridView
   **/
  @PostMapping("/loadMenuManagerLeftTreeJson")
  @ApiOperation(value="加载菜单管理左侧的部门树json接口", notes="加载菜单管理左侧的部门树json")
  public DataGridView loadPermissionManagerLeftTreeJson(PermissionVO permissionVO){
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("type",Constast.TYPE_MENU);
    List<Permission> permissions = permissionService.list(queryWrapper);
    List<TreeNode> treeNodes=new ArrayList<>();
    for (Permission permission : permissions) {
      boolean spread = permission.getOpen() == 1 ? true : false;
      treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),spread));
    }
    return new DataGridView(treeNodes);
  }

  /***
   *@Description: 全查询
   * @param permissionVO
   * @return: com.yxx.sys.common.DataGridView
   **/
  @GetMapping("/loadAllMenu")
  @ApiOperation(value="查询菜单接口", notes="查询菜单接口")
  public DataGridView loadAllMenu(PermissionVO permissionVO) {
    Page<Permission> page = new Page<>(permissionVO.getPage(),permissionVO.getLimit());
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(permissionVO.getId()!=null,"id",permissionVO.getId()).or().eq(permissionVO.getId()!=null,"pid",permissionVO.getId());
    queryWrapper.eq("type",Constast.TYPE_MENU);
    queryWrapper.like(StrUtil.isNotBlank(permissionVO.getTitle()),"title",permissionVO.getTitle());
    queryWrapper.orderByDesc("ordernum");
    permissionService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(),page.getRecords());
  }

  /***
   *@Description: 添加菜单
   * @param permissionVO
   * @return: com.yxx.sys.common.Result
   **/
  @PostMapping("/addMenu")
  @ApiOperation(value="添加菜单接口", notes="添加菜单")
  public Result addPermission(PermissionVO permissionVO) {
    try{
      permissionVO.setType(Constast.TYPE_MENU);//表明添加的是菜单
      permissionService.save(permissionVO);
      return Result.ADD_SUCCESS;
    }catch(Exception e){
      e.printStackTrace();
      return Result.ADD_REEOR;
    }
  }

  /***
   *@Description: 更新菜单
   * @param PermissionVO
   * @return: com.yxx.sys.common.Result
   **/
  @PostMapping("/updateMenu")
  @ApiOperation(value="更新菜单接口", notes="更新菜单")
  public Result updatePermission(PermissionVO PermissionVO) {
    try{
      permissionService.updateById(PermissionVO);
      return Result.UPDATE_SUCCESS;
    }catch(Exception e){
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }
  @PostMapping("/deleteMenu")
  @ApiOperation(value="删除菜单接口", notes="删除菜单")
  public Result deletePermission(PermissionVO PermissionVO) {
    try{
      permissionService.removeById(PermissionVO.getId());
      return Result.DELETE_SUCCESS;
    }catch(Exception e){
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }
  @GetMapping("/loadMenuMaxOrderNum")
  @ApiOperation(value="初始化排序码接口", notes="初始化排序码")
  public Map<String,Object> loadMenuMaxOrderNum() {
    Map<String,Object> map=new HashMap<>();
    LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.exists("SELECT max(t.ordernum) FROM `0810erp`.sys_dept t");
    map.put("value",permissionService.loadMaxOrderNum());
    return map;
  }

  @PostMapping("/checkMenuHasChildrenNode")
  @ApiOperation(value="检查当前id的菜单是否有子菜单接口", notes="初始化排序码")
  public Map<String,Object> checkPermissionHasChildrenNode(PermissionVO PermissionVO) {
    Map<String, Object> map = new HashMap<>();
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("pid",PermissionVO.getId());
    List<Permission> list = permissionService.list(queryWrapper);
    if(list.size()>0)
      map.put("value",true);
    else
      map.put("value",false);
    return map;
  }
}
